import json
import time
import os
import requests
from bs4 import BeautifulSoup
from time import sleep
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
from PIL import Image
from selenium.webdriver import ActionChains
import threading
from tqdm import tqdm

# mSession = requests.session()

url = "https://www.shipxy.com/ship/GetShipm"
# sliderUrl = "https://c.dun.163.com/api/v3/get?referer=https%3A%2F%2Fwww.shipxy.com%2F&zoneId=CN31&dt=BFPTG0QG2ONFAxUEAFeSD2CmrQM0hz7j&acToken=9ca17ae2e6ffcda170e2e6eea3b744a89ff8b3b379f6b08ba6d84e968f9eadc24dafbca98fdb3ab495a98ce92af0feaec3b92a96eca3a9e666fb8688a8c14b928f9bb3c54e908d8d8fdc7985ac8f98e743bcebee9e&id=361de48adf9b4cd8b5222e35b392fd74&fp=9LKWZttACE%5C2aYxok%2FSZBZ1Kg5WCBmTI%2Bm9g4MkKzn5zY%2FEVMa91tvekvDmVuLQVVKC%5CDo37pJtv8gyDL09u%2FokyR3nGYXv4cD%2FulCKt5aihfRp%2FO1oXjQWimeEJkeIYCvKTnpz8pez2DdoMoGJ9u%5CrTvb%5CcmaJQs1JqyNT%2Fg14ruCPS%3A1732292941834&https=true&type=undefined&version=2.27.2&dpr=1.25&dev=1&cb=irwc5E9tjOruEAcbN66YOQatCbgPYgyrH52STYQa%2FJacYcJ%2FlVTX88aSo%2BLCuUGCuSu05tz%2BNDdUMXDrjTgSGaWbDn87&ipv6=false&runEnv=10&group=&scene=&lang=zh-CN&sdkVersion=undefined&iv=4&width=320&audio=false&sizeType=10&smsVersion=v3&token=9375ece1490d4220888c1359164b8b06&callback=__JSONP_roglrl0_1"
# sliderUrl = "https://c.dun.163.com/api/v3/get?referer=https%3A%2F%2Fwww.shipxy.com%2F&zoneId=CN31&dt=BFPTG0QG2ONFAxUEAFeSD2CmrQM0hz7j&acToken=9ca17ae2e6ffcda170e2e6eea3b744a89ff8b3b379f6b08ba6d84e968f9eadc24dafbca98fdb3ab495a98ce92af0feaec3b92ab0a69db0f96e8cf18db0bb4f969f8bb3d14aa789888bcc79f4e787a9e947b799ee9e&id=361de48adf9b4cd8b5222e35b392fd74&fp=9LKWZttACE%5C2aYxok%2FSZBZ1Kg5WCBmTI%2Bm9g4MkKzn5zY%2FEVMa91tvekvDmVuLQVVKC%5CDo37pJtv8gyDL09u%2FokyR3nGYXv4cD%2FulCKt5aihfRp%2FO1oXjQWimeEJkeIYCvKTnpz8pez2DdoMoGJ9u%5CrTvb%5CcmaJQs1JqyNT%2Fg14ruCPS%3A1732292941834&https=true&type=undefined&version=2.27.2&dpr=1.25&dev=1&cb=rpF9KWeP2lNXyNrz3V2MS6FM3Nvsd.izg0GXKPFFD1Fg3%2FbIB5xGOQss.PzUb1t9.IsX%2Fuk2YrbWCUzX5o6OAJOByNM7&ipv6=false&runEnv=10&group=&scene=&lang=zh-CN&sdkVersion=undefined&iv=4&width=320&audio=false&sizeType=10&smsVersion=v3&token=dac28c2f07434bae9e64c585f79276ec&callback=__JSONP_00wtruz_3"

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0",

    "cookie":"FD857C2AF68165D4=ClDWSow7XQ1VLPkj36LpzAqCsjHB7Ji+5NI3muRSOTRr7NNi0h3SlY+ORafqOKzQ; _elane_rastar_tri=1; tc_TC=; _elane_shipfilter_sog=0%2C1; _elane_shipfilter_osog=; _elane_shipfilter_customsog=; _filter_flag=-1; _elane_shipfilter_one=2; Hm_lvt_adc1d4b64be85a31d37dd5e88526cc47=1732162746; HMACCOUNT=CB3234DA045B8790; tc_QX=; ASP.NET_SessionId=1pcwyk0qnui0mzddtr5jqu1i; _elane_shipfilter_olength=0; _elane_shipfilter_type=%u8D27%u8239%2C%u96C6%u88C5%u7BB1%u8239%2C%u6CB9%u8F6E%2C%u5F15%u822A%u8239%2C%u62D6%u8F6E%2C%u62D6%u5F15%2C%u6E14%u8239%2C%u6355%u635E%2C%u5BA2%u8239%2C%u641C%u6551%u8239%2C%u6E2F%u53E3%u4F9B%u5E94%u8239%2C%u88C5%u6709%u9632%u6C61%u88C5%u7F6E%u548C%u8BBE%u5907%u7684%u8239%u8236%2C%u6267%u6CD5%u8247%2C%u5907%u7528-%u7528%u4E8E%u5F53%u5730%u8239%u8236%u7684%u4EFB%u52A1%u5206%u914D%2C%u5907%u7528-%u7528%u4E8E%u5F53%u5730%u8239%u8236%u7684%u4EFB%u52A1%u5206%u914D%2C%u533B%u7597%u8239%2C%u7B26%u540818%u53F7%u51B3%u8BAE%28Mob-83%29%u7684%u8239%u8236%2C%u62D6%u5F15%u5E76%u4E14%u8239%u957F%3E200m%u6216%u8239%u5BBD%3E25m%2C%u758F%u6D5A%u6216%u6C34%u4E0B%u4F5C%u4E1A%2C%u6F5C%u6C34%u4F5C%u4E1A%2C%u53C2%u4E0E%u519B%u4E8B%u884C%u52A8%2C%u5E06%u8239%u822A%u884C%2C%u6E38%u8247%2C%u5730%u6548%u5E94%u8239%2C%u9AD8%u901F%u8239%2C%u5176%u4ED6%u7C7B%u578B%u7684%u8239%u8236%2C%u5176%u4ED6; _elane_shipfilter_length=0%2C40%2C41%2C80%2C81%2C120%2C121%2C160%2C161%2C240%2C241%2C320%2C321%2C9999; _elane_shipfilter_naviStatus=999%2C0%2C1%2C2%2C4%2C5%2C6%2C3%2C7%2C8%2C15%2C255; noTipApp=true; _elane_shipfilter_country=0%2C1%2C2; __snaker__id=G0Us3hDRNuRuEKpw; elane_language=%7B%22name%22%3A%22language%22%2C%22path%22%3A%22//api.shipxy.com/h5/api/languages/%22%2C%22mode%22%3A%22both%22%2C%22language%22%3A%22cn%22%7D; jfg=45818ba545b5c11dc1f403b9e8e5044d; token=f26a2b6c7f775951096dc3f9e6644c94; shipxy_v3_history_serch=s%u2606YEU5%u2606100000002%u2606%u2606IMO%uFF1A100000002%7Cs%u2606HAIPINHAO5299185%u2606100000002%u2606%u2606IMO%uFF1A100000002%7Cs%u2606YEU%20CHENG%20HAI%2045%20AD%u2606997786670%u2606%u2606MMSI%uFF1A997786670%7Cs%u2606ZHONGWEIJI0988%u2606413955237%u260670%u2606MMSI%uFF1A413955237%7Cs%u2606ZHONGWEIJI0988%u2606413955205%u260670%u2606MMSI%uFF1A413955205%7Cs%u2606ZHONGWEIJI0988%u2606413955205%u260670%u2606IMO%uFF1A35%7Cs%u2606YFDL5%u2606100000002%u2606%u2606IMO%uFF1A100000002%7Cs%u2606LIAOYINGYU%2036355%u2606400123354%u2606%u2606MMSI%uFF1A400123354%7Cs%u2606ADFISHBUOY14%2097%u2606100044414%u260630%u2606MMSI%uFF1A100044414%7Cs%u2606ZHEYINYU11655897%u2606100000004%u2606%u2606MMSI%uFF1A100000004; gdxidpyhxdE=QnQvRykBGNGEyQ%2BAen7xtJducWPWNwX2GpYhcXZ0XrsMBEdljucQNVNgdKIMhTEUPjrfqKDbzP%2BRKWIfuvca6SGs3AIfzhxpiLbQIWc7E7kOy4sBIlnMvJ4fL3MRB8vU%5CV9x9ATqUDM03syW%2F%5CIQN1Lffb%2FBHrn2UuHoMaDg%5C9Ls%2FjZg%3A1732518502870; .UserAuth2=F1B93A7E29B227A22E93FD3809A1318E5AFC64F9F99A4079270CBCA7207C4193DAECE86B51EA7CCA31F6061E5D76D99987336F94CD12AF9E09DBB73FB45D7DDBBA66256D73AF63AEE16CADCAEF27763071E6DF7A693FBD1D216336D103CA3987B9C8C07BCC49FC7D6DB352BEA82ED0B7EA66B7001A05700A2B8B9907FC9050B42BAC230B57BA7A1F4100FC23818E1F232C55304D; UserAuthUCenter=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImtfS1JMRk9TM1dPYk9ZcUN2ZEpKS2I3ZGo1TSIsImtpZCI6ImtfS1JMRk9TM1dPYk9ZcUN2ZEpKS2I3ZGo1TSJ9.eyJpc3MiOiJodHRwOi8vaWQyLmVsYW5lLmNvbS5jbi9jb3JlIiwiYXVkIjoiaHR0cDovL2lkMi5lbGFuZS5jb20uY24vY29yZS9yZXNvdXJjZXMiLCJleHAiOjE3NDAyOTM4MTksIm5iZiI6MTczMjUxNzgxOSwiY2xpZW50X2lkIjoiSFlRX1Jlc19BbGxJbiIsInNjb3BlIjpbIm9wZW5pZCIsInByb2ZpbGUiLCJ1c2VyYXBpIl0sInN1YiI6IjMyOThhYWUxNjdkMGU1OTkiLCJhdXRoX3RpbWUiOjE3MzI1MTc4MTksImlkcCI6Imlkc3J2IiwiYW1yIjpbInBhc3N3b3JkIl19.tO0ZJG8jIUxMa6YRFPSnthfxSbVlLYFiMZq4xmNprOQxDmLHTpqu3WisFPQjg-zs6syQNCNfGnUwW0uWqPUcTDrnjAIK8BfNOeJHf1jUOp_lwHLaEMK-bGNXWVXEUKSPU_ImhulTLM0tvtubMHL1v-ZE8vy-TC95lWL4Le7Cl5JD-BovuzKex9Y9RdKh0UONJCUJGY6mkUmvx7sFFZfCC7VzaDYNk-WZzJA3ZuJ1clHwCLs8pkM2shLaloi2RS8vDGHRpGEfPTUzzs7LWjL-uNmd0F9wMvgSYzeFi6-CQiHQ-KcuRc4xS9cW0AYQGwUMhr24JA1ckC5d6GyKEMqQVA; Hm_lpvt_adc1d4b64be85a31d37dd5e88526cc47=1732517820; SERVERID=eef51467a6eba599663cd14d5e759568|1732517822|1732457966",
    "referer": "https://www.shipxy.com/",

}

# cDic = [
#     {"name":"SERVERID","value":"eef51467a6eba599663cd14d5e759568|1732481548|1732457966"},
#     {"name":"Path","value":"/"}
# ]

# cookieDicList = [
#     {"name": "FD857C2AF68165D4", "value": "ClDWSow7XQ1VLPkj36LpzAqCsjHB7Ji+5NI3muRSOTRr7NNi0h3SlY+ORafqOKzQ"},
#     {"name": "_elane_rastar_tri", "value": "1"},
#     {"name": "tc_TC", "value": ""},
#     {"name": "_elane_shipfilter_sog", "value": "0%2C1"},
#     {"name": "_elane_shipfilter_osog", "value": ""},
#     {"name": "_elane_shipfilter_customsog", "value": ""},
#     {"name": "_filter_flag", "value": "-1"},
#     {"name": "_elane_shipfilter_one", "value": "2"},
#     {"name": "Hm_lvt_adc1d4b64be85a31d37dd5e88526cc47", "value": "1732162746"},
#     {"name": "HMACCOUNT", "value": "CB3234DA045B8790"},
#     {"name": "tc_QX", "value": ""},
#     {"name": "ASP.NET_SessionId", "value": "1pcwyk0qnui0mzddtr5jqu1i"},
#     {"name": "_elane_shipfilter_olength", "value": "0"},
#     {"name": "_elane_shipfilter_type", "value": "%u8D27%u8239%2C%u96C6%u88C5%u7BB1%u8239%2C%u6CB9%u8F6E%2C%u5F15%u822A%u8239%2C%u62D6%u8F6E%2C%u62D6%u5F15%2C%u6E14%u8239%2C%u6355%u635E%2C%u5BA2%u8239%2C%u641C%u6551%u8239%2C%u6E2F%u53E3%u4F9B%u5E94%u8239%2C%u88C5%u6709%u9632%u6C61%u88C5%u7F6E%u548C%u8BBE%u5907%u7684%u8239%u8236%2C%u6267%u6CD5%u8247%2C%u5907%u7528-%u7528%u4E8E%u5F53%u5730%u8239%u8236%u7684%u4EFB%u52A1%u5206%u914D%2C%u5907%u7528-%u7528%u4E8E%u5F53%u5730%u8239%u8236%u7684%u4EFB%u52A1%u5206%u914D%2C%u533B%u7597%u8239%2C%u7B26%u540818%u53F7%u51B3%u8BAE%28Mob-83%29%u7684%u8239%u8236%2C%u62D6%u5F15%u5E76%u4E14%u8239%u957F%3E200m%u6216%u8239%u5BBD%3E25m%2C%u758F%u6D5A%u6216%u6C34%u4E0B%u4F5C%u4E1A%2C%u6F5C%u6C34%u4F5C%u4E1A%2C%u53C2%u4E0E%u519B%u4E8B%u884C%u52A8%2C%u5E06%u8239%u822A%u884C%2C%u6E38%u8247%2C%u5730%u6548%u5E94%u8239%2C%u9AD8%u901F%u8239%2C%u5176%u4ED6%u7C7B%u578B%u7684%u8239%u8236%2C%u5176%u4ED6"},
#     {"name": "_elane_shipfilter_length", "value": "0%2C40%2C41%2C80%2C81%2C120%2C121%2C160%2C161%2C240%2C241%2C320%2C321%2C9999"},
#     {"name": "_elane_shipfilter_naviStatus", "value": "999%2C0%2C1%2C2%2C4%2C5%2C6%2C3%2C7%2C8%2C15%2C255"},
#     {"name": "noTipApp", "value": "true"},
#     {"name": "_elane_shipfilter_country", "value": "0%2C1%2C2"},
#     {"name": "__snaker__id", "value": "G0Us3hDRNuRuEKpw"},
#     {"name": "elane_language", "value": "%7B%22name%22%3A%22language%22%2C%22path%22%3A%22//api.shipxy.com/h5/api/languages/%22%2C%22mode%22%3A%22both%22%2C%22language%22%3A%22cn%22%7D"},
#     {"name": "jfg", "value": "45818ba545b5c11dc1f403b9e8e5044d"},
#     {"name": "UserAuthUCenter", "value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImtfS1JMRk9TM1dPYk9ZcUN2ZEpKS2I3ZGo1TSIsImtpZCI6ImtfS1JMRk9TM1dPYk9ZcUN2ZEpKS2I3ZGo1TSJ9.eyJpc3MiOiJodHRwOi8vaWQyLmVsYW5lLmNvbS5jbi9jb3JlIiwiYXVkIjoiaHR0cDovL2lkMi5lbGFuZS5jb20uY24vY29yZS9yZXNvdXJjZXMiLCJleHAiOjE3NDAwMDg2NDAsIm5iZiI6MTczMjIzMjY0MCwiY2xpZW50X2lkIjoiSFlRX1Jlc19BbGxJbiIsInNjb3BlIjpbIm9wZW5pZCIsInByb2ZpbGUiLCJ1c2VyYXBpIl0sInN1YiI6Ijg1Y2UzN2FkY2RlMzcyYjQiLCJhdXRoX3RpbWUiOjE3MzIyMzI2NDAsImlkcCI6Imlkc3J2IiwiYW1yIjpbInBhc3N3b3JkIl19.C93v4TLUExpJkl3CV4q1K35-dVHFKnh3lBwQ9AgQhOg5FOTf5_bYvOrtpVU_a1bhycB4awWW745x-M5nJp7SnGNRN6toHRdhxB2hR_Vz-YFZeRBO44oeyKWH_aDiCCNcXDhJfyIEiC7tT7oJi6DA2YMLmlf5rmst_PQ6gF4mOBTblZOvnaCaqW9r5B8uv7f2JLTYhi6ri3PnWi8SIrxoZoZL9xojv4cA8wI409zUpYVZabPzIbRCkEcy8NDKSuAHsby98fuLAvKtL8k30A_1xn4HS71S-S6klFaun0EZEZmbI3f3xBATEHYcJB5nV1iiv5lab68tGei707wUiBG3pg"},
#     {"name": "token", "value": "f26a2b6c7f775951096dc3f9e6644c94"},
#     {"name": ".UserAuth2", "value": "8D7D3058D5CFA1952D198AE27623E64477248C8D92E4746C5BC1C0C7F7CCA5FBD0C985D1985F67015B34AD4D01F3009CC24119C6A9B761DC994D86E67F79460209024457ECF364507EC4844CD92C2BBC77B5D7E9D8A3F1EC2DF2818186080C56A69F489FE7CB77A27359E39B824F23342CDF6652BBEBF408C002C29710B38CEC7B6B5CA7E4ABD6D0EA42387F7D253471609C777B0DE9BBED573CD224B748B2E8C2948CE4"},
#     {"name": "shipxy_v3_history_serch", "value": "s%u2606HAIPINHAO5299185%u2606100000002%u2606%u2606IMO%uFF1A100000002%7Cs%u2606YFDL5%u2606100000002%u2606%u2606IMO%uFF1A100000002%7Cs%u2606ADFISHBUOY14%2097%u2606100044414%u260630%u2606MMSI%uFF1A100044414%7Cs%u2606ZHEYINYU11655897%u2606100000004%u2606%u2606MMSI%uFF1A100000004%7Cs%u2606HAIPINHAO5299193%u2606100000002%u2606%u2606IMO%uFF1A100000002%7Cs%u2606HAIPINHAO5299194%u2606100000002%u2606%u2606IMO%uFF1A100000002%7Cs%u2606ZHEPUYU04281%u2606400004281%u260630%u2606MMSI%uFF1A400004281%7Cs%u260607326958%u2606400003306%u2606%u2606MMSI%uFF1A400003306%7Cs%u2606000%u2606400001188%u260670%u2606MMSI%uFF1A400001188%7Cs%u2606YU01305%u2606400001305%u260630%u2606MMSI%uFF1A400001305"},
#     {"name": "Hm_lpvt_adc1d4b64be85a31d37dd5e88526cc47", "value": "1732405371"},
#     {"name": "gdxidpyhxdE", "value": "ij9ckIVnPOZnJQmKke%2Fph%5Cjw9er0mwVhPpOvULk6GUtHwuzLQ0ziaogc0zhRRR4xytp40R7d43Rlb38RibiaCJ8v4W%2BOmgGHLCM52MikbcSdWtzGVRlXYrxJJio004e97COHpmzXuYJQytLJ%2FKQTuzPURez%5CPLf3MKM9jA5nxs17Xyob%3A1732418933118"},
#     {"name": "SERVERID", "value": "eef51467a6eba599663cd14d5e759568|1732488187|1732457966}"}
#     # {"name": "SERVERID", "value": "9d5fe2e75816d586ec8202c8a7e5ae11|1732418796|1732418734}"}
#     ]

# sliderData = {
#     "referer": " https://www.shipxy.com/",
#     "zoneId": "CN31",
#     "dt": "BFPTG0QG2ONFAxUEAFeSD2CmrQM0hz7j",
#     "acToken": "9ca17ae2e6ffcda170e2e6eea3b744a89ff8b3b379f6b08ba6d84e968f9eadc24dafbca98fdb3ab495a98ce92af0feaec3b92a96eca3a9e666fb8688a8c14b928f9bb3c54e908d8d8fdc7985ac8f98e743bcebee9e",
#     "id": "361de48adf9b4cd8b5222e35b392fd74",
#     "fp": r"9LKWZttACE\2aYxok/SZBZ1Kg5WCBmTI+m9g4MkKzn5zY/EVMa91tvekvDmVuLQVVKC\Do37pJtv8gyDL09u/okyR3nGYXv4cD/ulCKt5aihfRp/O1oXjQWimeEJkeIYCvKTnpz8pez2DdoMoGJ9u\rTvb\cmaJQs1JqyNT/g14ruCPS:1732292941834",
#     "https": "true",
#     "type": "undefined",
#     "version": "2.27.2",
#     "dpr": "1.25",
#     "dev": 1,
#     "cb": "irwc5E9tjOruEAcbN66YOQatCbgPYgyrH52STYQa/JacYcJ/lVTX88aSo+LCuUGCuSu05tz+NDdUMXDrjTgSGaWbDn87",
#     "ipv6": "false",
#     "runEnv": 10,
#     "group": "",
#     "scene": "",
#     "lang": "zh-CN",
#     "sdkVersion": "undefined",
#     "iv": "4",
#     "width": "320",
#     "audio": "false",
#     "sizeType": "10",
#     "smsVersion": "v3",
#     "token": "9375ece1490d4220888c1359164b8b06",
#     "callback": "__JSONP_roglrl0_1"
# }

# 登录
# loginUrl = "https://www.shipxy.com/Home/Login"
# loginData = {
#     "username": "wuyaofanjuan",
#     "password": "wuyao830407",
#     "autologin": "true",
#     "authCode": ""
# }
# loginResp = mSession.post(loginUrl,data=loginData,headers=headers)
# print(loginResp.text)
# print(loginResp.cookies)

# getCount = 0
noGetMMSI = []
recordDic = {
    "getMMSI": 0,
    "noGetList": []
}
# ii = 0
start_mmsi = 400000000
# data_folder_path = "C:/Users/fanyf/Desktop/ship/"
lock = threading.Lock()

shipFilePath = "C:/Users/fanyf/Desktop/ship/shipData.csv"
# isShipFileExist = os.path.exists(shipFilePath)
#
# if isShipFileExist:
#     f = open(shipFilePath, mode="a", encoding='utf-8', newline='')
# else:
#     f = open(shipFilePath, mode="w", encoding='utf-8', newline='')
#
# csvwriter = csv.writer(f)

recordFilePath = "C:/Users/fanyf/Desktop/ship/record.json"
if os.path.exists(recordFilePath):
    with open(recordFilePath, 'r') as file:
        data = json.load(file)
    noGetMMSI.extend(data["noGetList"])
    # print(noGetMMSI)
    start_mmsi = data["getMMSI"]
    print("找到爬取文件，当前mmsi：", start_mmsi)
else:
    print("不存在爬取记录文件")




# request 获取返回值
def get_resp(m_url, m_data, m_headers):
    resp = requests.post(m_url, data=m_data, headers=m_headers)
    resp.encoding = 'utf-8'
    # resp = mSession.post(m_url, data=data, headers=headers)
    text = resp.text
    # print(text)

    if "验证码" in text:
        print("有验证码")
        resp.close()

        # # 1、隐藏Edge 正受到自动测试软件的控制
        # edge_options = webdriver.EdgeOptions()
        # edge_options.add_experimental_option('excludeSwitches', ['enable-automation'])
        # browser = webdriver.Edge(options=edge_options)
        #
        # # 2、打开浏览器
        # my_url = "https://www.shipxy.com"
        # browser.get(my_url)  # 打开浏览器
        #
        # browser.maximize_window()  # 将浏览器全屏
        # time.sleep(2)
        # for c in cookieDicList:
        #     browser.add_cookie(c)
        #
        # browser.get(my_url)  # 打开浏览器
        #
        # time.sleep(200)
        # 3、输入mmsi号
        # search_input = browser.find_element(By.XPATH, "/html/body/form/div/div[2]/div[1]/input[1]")  # 运用Xpath方法，找到查询的输入框
        # search_input.send_keys("1111")

        # 4、点击搜索哦按钮
        # button = browser.find_element(By.XPATH, '/html/body/form/div/div[3]/input[5]')  # 运用Xpath方法，找到搜索的按钮
        # button.click()  # 点击按钮
        # time.sleep(2)  # 等待2秒

        # 5、全屏截图
        # slider_request = requests.get(sliderUrl,headers=headers)
        # print(json.loads(slider_request.text.split("(")[1].split(")")[0]))
        return 0
    else:
        # print(resp.text)
        t = resp.json()["data"][0]
        resp.close()
        return t


def single_thread_spider(m_url, m_headers,start_mmsi_,num_):
    # if sel == "2":
    #     print("开始爬取之前没爬取的")
    #     for m in noGetMMSI:
    #         # print(m)
    #         data = {
    #             "mmsi": int(m)
    #         }
    #
    #         try:
    #             result = get_resp(url, data, headers)
    #
    #             if result == 0:
    #                 break
    #             else:
    #                 if not isShipFileExist:
    #                     csvwriter.writerow(result.keys())
    #                 csvwriter.writerow(result.values())
    #
    #                 noGetMMSI.remove(m)
    #                 recordDic['noGetList'] = noGetMMSI
    #                 with open(recordFilePath, 'w') as file:
    #                     file.write(json.dumps(recordDic))
    #
    #                 getCount += 1
    #                 print(m, "获取成功，已获取", getCount, "个，剩余：", len(noGetMMSI))
    #
    #
    #
    #         except IndexError:
    #             print(m, "没有获取到数据，已获取", getCount, "个，剩余：", len(noGetMMSI))
    #
    #         sleep(0.1)
    #
    # else:
    get_count = 0
    current_mmsi =start_mmsi
    for i in tqdm(range(num_)):
        current_mmsi = start_mmsi_ + i
        with lock:
            # print(current_mmsi, recordDic["getMMSI"])
            if current_mmsi > recordDic["getMMSI"]:
                recordDic["getMMSI"] = current_mmsi
                # print("已更新")
            with open(recordFilePath, 'w') as f:
                f.write(json.dumps(recordDic))

        # print('正在获取', current_mmsi)
        data_ = {
            "mmsi": current_mmsi
        }

        try:
            result = get_resp(m_url, data_, m_headers)

            if result == 0:
                break
            else:
                # 数据写入文件
                with lock:
                    recordDic["noGetList"].append(current_mmsi)
                    is_ship_file_exist = os.path.exists(shipFilePath)

                    if is_ship_file_exist:
                        f = open(shipFilePath, mode="a", encoding='utf-8', newline='')
                    else:
                        f = open(shipFilePath, mode="w", encoding='utf-8', newline='')

                    csvwriter = csv.writer(f)

                    if not is_ship_file_exist:
                        csvwriter.writerow(result.keys())
                    csvwriter.writerow(result.values())
                    f.close()



                # 计数
                get_count += 1
                # print(current_mmsi, "获取成功，已获取", get_count, "/", i, "个")


        except IndexError:
            noGetMMSI.append(current_mmsi)
            recordDic['noGetList'] = noGetMMSI
            with lock:
                with open(recordFilePath, 'w') as f:
                    f.write(json.dumps(recordDic))

            # print(current_mmsi, "没有获取到数据，已获取", get_count, "/", i, "个")

        sleep(0.1)

    print("本线程最后一个mmsi：",current_mmsi)


def mult_thread_spider():
    sel = input("继续爬取请输入1，爬取之前失败的请输入2：")
    thread_num = input("请输入爬取线程数：")
    num = input("请输入每个线程爬取个数：")
    threads = []
    for i in range(int(thread_num)):
        threads.append(
            threading.Thread(target=single_thread_spider, args=(url,headers,start_mmsi+i*int(num),int(num)))
        )

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()





if __name__ == "__main__":
    mult_thread_spider()
    with open(recordFilePath, 'r', encoding='utf-8') as f:
        recordDic = json.load(f)
    print("all over!!!，最后一个mmsi：",recordDic["getMMSI"])
